package org.china.mobile.device;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.ext.bridge.PermittedOptions;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.StaticHandler;
import io.vertx.ext.web.handler.sockjs.SockJSBridgeOptions;
import io.vertx.ext.web.handler.sockjs.SockJSHandler;

public class Main  extends AbstractVerticle {


    // 写法1

//    public void start() {
//        vertx.createHttpServer().requestHandler(req->{
//            String uri = req.uri();
//            String result = "Hello world";
//            if(uri.startsWith("/wwww")){
//                result = "Hello wwww";
//            }
//
//            req.response().putHeader("content-type","text/plain").end(result);
//        }).listen(8080);
//    }

    // 写法2
    public void start(){
        HttpServer server = vertx.createHttpServer();

        Router router = Router.router(vertx);



        PermittedOptions inboundPermitted = new PermittedOptions().setAddress("chatroom");
        SockJSHandler sockJSHandler = SockJSHandler.create(vertx);
        SockJSBridgeOptions sockJSBridgeOptions = new SockJSBridgeOptions()
                .addInboundPermitted(inboundPermitted)
                .addOutboundPermitted(inboundPermitted);

        router.route("/eventbus/*").subRouter(sockJSHandler.bridge(sockJSBridgeOptions));

        router.route().handler(StaticHandler.create());

        server.requestHandler(router).listen(8080);
    }


    public static void main(String[] args) {
        Vertx.vertx().deployVerticle(new Main());
    }
}